﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.ComponentModel;
using DevExpress.XtraEditors.Popup;

namespace SAF.Controls
{
    [ToolboxItem(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    public class TreeLookUpEdit : LookUpEditBase
    {
        static TreeLookUpEdit()
        {
            RepositoryItemTreeLookUpEdit.RegisterTreeLookUpEdit();
        }

        [Category("Properties"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemTreeLookUpEdit Properties
        {
            get
            {
                return base.Properties as RepositoryItemTreeLookUpEdit;
            }
        }

        public override string EditorTypeName
        {
            get
            {
                return "TreeLookUpEdit";
            }
        }

        protected override PopupBaseForm GetPopupForm()
        {
            PopupBaseForm form = base.GetPopupForm();
            if (form != null) Properties.SetupLookUpPopup();
            return form;
        }

        internal PopupBaseForm PopupFormCore
        {
            get
            {
                return PopupForm;
            }
        }

        private object onAcceptEditValue;
        protected override void AcceptPopupValue(object val)
        {
            if (val != null)
            {
                this.onAcceptEditValue = this.EditValue;
                this.SetEmptyEditValue(null);
            }
            this.isDisplayTextValid = true;
            base.AcceptPopupValue(val);
            this.onAcceptEditValue = null;
        }

        protected override bool IsAcceptCloseMode(PopupCloseMode closeMode)
        {
            return closeMode == PopupCloseMode.Normal || closeMode == PopupCloseMode.ButtonClick;
        }

        protected new PopupTreeLookUpEditForm PopupForm
        {
            get
            {
                return base.PopupForm as PopupTreeLookUpEditForm;
            }
        }

        protected override bool CheckInputNewValue(bool partial)
        {
            if (!this.IsMaskBoxAvailable)
            {
                return true;
            }
            return false;
        }

        public override object GetSelectedDataRow()
        {
            return this.Properties.GetNodeByKeyValue(this.EditValue);
        }

        internal void RefreshCore(bool resetCache)
        {
            this.Refresh(resetCache);
        }

        protected override void Refresh(bool resetCache)
        {
            base.ViewInfo.RefreshDisplayText = true;
            base.Refresh();
        }

        protected override object GetChangingOldEditValue(object value)
        {
            if (this.onAcceptEditValue != null)
            {
                return this.onAcceptEditValue;
            }
            return value;
        }
        protected override void OnValidatingCore(CancelEventArgs e)
        {
            if (!this.CompareEditValue(this.EditValue, this.lastChangedEditValue, false))
            {
                this.OnEditValueChanged();
            }
            base.OnValidatingCore(e);
        }

        protected override void OnMouseDownClosePopup()
        {
            this.ClosePopup(PopupCloseMode.Cancel);
        }
        protected override void ParseEditorValue()
        {
            this.AutoSearchText = string.Empty;
            this.isDisplayTextValid = true;
            if (this.IsMaskBoxAvailable)
            {
                this.UpdateMaskBoxDisplayText();
            }
        }

        protected override void SetEmptyEditValue(object emptyEditValue)
        {
            this.isDisplayTextValid = true;
            base.SetEmptyEditValue(emptyEditValue);
        }

        private bool prevHideSelection = true;

        protected override void OnPopupShown()
        {
            this.prevHideSelection = this.Properties.HideSelection;
            this.UpdateHideSelection(false);
            base.OnPopupShown();
        }

        protected override void OnPopupClosed(PopupCloseMode closeMode)
        {
            this.isDisplayTextValid = true;
            this.UpdateHideSelection(this.prevHideSelection);
            if (closeMode != PopupCloseMode.Immediate && this.EditorContainsFocus && this.MaskBox != null)
            {
                this.MaskBox.Focus();
            }
            base.OnPopupClosed(closeMode);
        }

        private void UpdateHideSelection(bool newValue)
        {
            this.Properties.BeginUpdate();
            this.Properties.HideSelection = newValue;
            this.Properties.CancelUpdate();
            if (this.MaskBox != null)
            {
                this.MaskBox.HideSelection = newValue;
            }
        }

        protected override PopupBaseForm CreatePopupForm()
        {
            return new PopupTreeLookUpEditForm(this);
        }

        public override void ShowPopup()
        {
            if (!this.IsPopupOpen)
            {
                this.Properties.ActivateDataSource();
            }
            base.ShowPopup();
        }

        private bool isDisplayTextValid = true;
        protected internal virtual bool IsDisplayTextValid
        {
            get
            {
                return this.isDisplayTextValid;
            }
        }

    }
}
